package wu2020.top.compare.orm.demo.infrastructure.persistence.mybatis;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wu.framework.inner.lazy.database.expand.database.persistence.domain.LazyPage;
import com.wu.framework.response.Result;
import com.wu.framework.response.ResultFactory;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import wu2020.top.compare.orm.demo.domain.model.sys.user.SysUser;
import wu2020.top.compare.orm.demo.domain.model.sys.user.ORMComparisonRepository;
import wu2020.top.compare.orm.demo.infrastructure.converter.SysUserConverter;
import wu2020.top.compare.orm.demo.infrastructure.entity.SysUserDO;
import wu2020.top.compare.orm.demo.infrastructure.mapper.mybatis.SysUserMapper;
import wu2020.top.compare.orm.demo.infrastructure.persistence.SysUserRepositoryAbstractRecord;
import wu2020.top.compare.orm.demo.infrastructure.persistence.enums.Orm;
import wu2020.top.compare.orm.demo.infrastructure.persistence.enums.Type;

import java.time.LocalDateTime;
import java.util.List;

/**
 * describe sys_user
 *
 * @author Jia wei Wu
 * @date 2024/02/28 11:27 上午
 * @see com.wu.framework.inner.lazy.persistence.reverse.lazy.ddd.DefaultDDDLazyInfrastructurePersistence
 **/
@Component("mybatis")
public  class MybatisComparisonRepository extends SysUserRepositoryAbstractRecord
        implements ORMComparisonRepository {

    @Resource
    SysUserMapper sysUserMapper;

    /**
     * describe 新增
     *
     * @param sysUser 新增
     * @return {@link Result<SysUser>} 新增后领域对象
     * @author Jia wei Wu
     * @date 2024/02/28 11:27 上午
     **/

    @Override
    public Result<SysUser> story(SysUser sysUser) throws Exception {
        boolean success = true;
        super.resetTestTableRecords();
        LocalDateTime startTime = LocalDateTime.now();
        try {
            SysUserDO sysUserDO = SysUserConverter.INSTANCE.fromSysUser(sysUser);
            sysUserMapper.story(sysUserDO);
        } catch (Exception e) {
            e.printStackTrace();
            success = false;
        }

        LocalDateTime endTime = LocalDateTime.now();
        storyRecord(Orm.MYBATIS, Type.story, startTime, endTime, success);
        return ResultFactory.successOf();
    }

    /**
     * describe 批量新增
     *
     * @param sysUserList 批量新增
     * @return {@link Result<List<SysUser>>} 新增后领域对象集合
     * @author Jia wei Wu
     * @date 2024/02/28 11:27 上午
     **/

    @Override
    public Result<List<SysUser>> batchStory(List<SysUser> sysUserList) throws Exception {
        boolean success = true;
                super.resetTestTableRecords();
        LocalDateTime startTime = LocalDateTime.now();
        try {
            List<SysUserDO> sysUserDOList = sysUserList.stream().map(SysUserConverter.INSTANCE::fromSysUser).toList();
            sysUserMapper.batchStory(sysUserDOList);
        } catch (Exception e) {
            e.printStackTrace();
            success = false;
        }


        LocalDateTime endTime = LocalDateTime.now();
        storyRecord(Orm.MYBATIS, Type.batchStory, startTime, endTime, sysUserList.size(), success);
        return ResultFactory.successOf();
    }

    /**
     * describe 查询单个
     *
     * @param sysUser 查询单个
     * @return {@link Result<SysUser>} 领域对象
     * @author Jia wei Wu
     * @date 2024/02/28 11:27 上午
     **/

    @Override
    public Result<SysUser> findOne(SysUser sysUser) throws Exception {
        boolean success = true;
        super.findOne(sysUser);
        LocalDateTime startTime = LocalDateTime.now();
        try {
            LambdaQueryWrapper<SysUserDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(!ObjectUtils.isEmpty(sysUser.getUsername()), SysUserDO::getUsername, sysUser.getUsername());
            lambdaQueryWrapper.eq(!ObjectUtils.isEmpty(sysUser.getId()), SysUserDO::getId, sysUser.getId());

            // ... 逐一添加
            SysUserDO sysUserDO = sysUserMapper.selectOne(lambdaQueryWrapper);
            SysUser sysUserOne = SysUserConverter.INSTANCE.toSysUser(sysUserDO);
            return ResultFactory.successOf(sysUserOne);
        } catch (Exception e) {
            e.printStackTrace();
            success = false;
        }finally {
            LocalDateTime endTime = LocalDateTime.now();
            storyRecord(Orm.MYBATIS, Type.findOne, startTime, endTime, success);
        }
        return ResultFactory.successOf();
    }

    /**
     * describe 查询多个
     *
     * @param sysUser 查询多个
     * @return {@link Result<List<SysUser>>} 领域对象
     * @author Jia wei Wu
     * @date 2024/02/28 11:27 上午
     **/

    @Override
    public Result<List<SysUser>> findList(SysUser sysUser) throws Exception {
        boolean success = true;
        super.findList(sysUser);
        LocalDateTime startTime = LocalDateTime.now();
        try {
            LambdaQueryWrapper<SysUserDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(!ObjectUtils.isEmpty(sysUser.getUsername()), SysUserDO::getUsername, sysUser.getUsername());
            lambdaQueryWrapper.eq(!ObjectUtils.isEmpty(sysUser.getId()), SysUserDO::getId, sysUser.getId());

            // ... 逐一添加

            List<SysUserDO> sysUserDOList = sysUserMapper.selectList(lambdaQueryWrapper);

            List<SysUser> sysUserList = sysUserDOList.stream().map(SysUserConverter.INSTANCE::toSysUser).toList();
            return ResultFactory.successOf(sysUserList);
        } catch (Exception e) {
            e.printStackTrace();
            success = false;
        } finally {
            LocalDateTime endTime = LocalDateTime.now();
            storyRecord(Orm.MYBATIS, Type.findList, startTime, endTime, success);

        }
        return ResultFactory.successOf();

    }

    /**
     * describe 分页查询多个
     *
     * @param size    当前页数
     * @param current 当前页
     * @param sysUser 分页查询多个
     * @return {@link Result<LazyPage<SysUser>>} 分页领域对象
     * @author Jia wei Wu
     * @date 2024/02/28 11:27 上午
     **/

    @Override
    public Result<LazyPage<SysUser>> findPage(int size, int current, SysUser sysUser) throws Exception {
        boolean success = true;
        super.findList(sysUser);
        LocalDateTime startTime = LocalDateTime.now();
        try {
            Page<SysUserDO> page = new Page<>(current, size);

            LambdaQueryWrapper<SysUserDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(!ObjectUtils.isEmpty(sysUser.getUsername()), SysUserDO::getUsername, sysUser.getUsername());
            lambdaQueryWrapper.eq(!ObjectUtils.isEmpty(sysUser.getId()), SysUserDO::getId, sysUser.getId());

            // ... 逐一添加
            Page<SysUserDO> sysUserDOPage = sysUserMapper.selectPage(page, lambdaQueryWrapper);

            long pages = sysUserDOPage.getPages();
            List<SysUserDO> records = sysUserDOPage.getRecords();
            long total = sysUserDOPage.getTotal();

            LazyPage<SysUser> lazyPage = new LazyPage<>(current, size);
            lazyPage.setTotal(total);
            lazyPage.setRecord(records.stream().map(SysUserConverter.INSTANCE::toSysUser).toList());
            return ResultFactory.successOf(lazyPage);
        } catch (Exception e) {
            e.printStackTrace();
            success = false;
        } finally {
            LocalDateTime endTime = LocalDateTime.now();
            storyRecord(Orm.MYBATIS, Type.findPage, startTime, endTime, size,success);
        }
        return ResultFactory.successOf();
    }

    /**
     * describe 删除
     *
     * @param sysUser 删除
     * @return {@link Result<SysUser>}
     * @author Jia wei Wu
     * @date 2024/02/28 11:27 上午
     **/

    @Override
    public Result<SysUser> remove(SysUser sysUser) throws Exception {
        boolean success = true;
        super.remove(sysUser);
        LocalDateTime startTime = LocalDateTime.now();
        try {
            LambdaQueryWrapper<SysUserDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(!ObjectUtils.isEmpty(sysUser.getUsername()), SysUserDO::getUsername, sysUser.getUsername());
            lambdaQueryWrapper.eq(!ObjectUtils.isEmpty(sysUser.getId()), SysUserDO::getId, sysUser.getId());

            // ... 逐一添加

            sysUserMapper.delete(lambdaQueryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            success = false;
        }

        LocalDateTime endTime = LocalDateTime.now();
        storyRecord(Orm.MYBATIS, Type.remove, startTime, endTime, success);
        return ResultFactory.successOf();
    }

    /**
     * describe 是否存在
     *
     * @param sysUser 领域对象
     * @return {@link Result<Boolean>} 是否存在 true 存在，false 不存在
     * @author Jia wei Wu
     * @date 2024/02/28 11:27 上午
     **/

    @Override
    public Result<Boolean> exists(SysUser sysUser) throws Exception {
        boolean success = true;
        super.exists(sysUser);
        boolean exists = false;
        LocalDateTime startTime = LocalDateTime.now();
        try {
            LambdaQueryWrapper<SysUserDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(!ObjectUtils.isEmpty(sysUser.getUsername()), SysUserDO::getUsername, sysUser.getUsername());
            lambdaQueryWrapper.eq(!ObjectUtils.isEmpty(sysUser.getId()), SysUserDO::getId, sysUser.getId());

            // ... 逐一添加
            exists = sysUserMapper.exists(lambdaQueryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            success = false;
        }


        LocalDateTime endTime = LocalDateTime.now();
        storyRecord(Orm.MYBATIS, Type.exists, startTime, endTime, success);
        return ResultFactory.successOf(exists);
    }

}